/**
 * Anonymous self-executing object for Programmer Tools section of the DRY Control
 */
(function() {
	
	var loaded_aliases = {};
	
	donrey.load_CSS("/dryfe/css/donrey-modules.css");

	$('#donrey-modules a.icon').click(function() {
		var	module_info = $(this).data('info');
		
		if(!loaded_aliases[module_info.alias]) {
			// create a "Module" tab
			loaded_aliases[module_info.alias] = donrey.control.add_Tab('/control/?cmd=Load_Module_Interface&alias=' + module_info.alias, 'Module: '+module_info.alias);
		} else
			// if already created for this alias, just select it
			donrey.control.select_Tab(loaded_aliases[module_info.alias]);
	});
	
	return {
		init: function() {
			donrey.control.attach_Observer(this);
		},
		// Observer pattern
		update: function(event, data) {
			if(event == 'tab-close') {
				// scan all, delete the one affected and reduce "index" number for the rest
				for(var alias in loaded_aliases)
					if(data == loaded_aliases[alias])
						delete loaded_aliases[alias];
					else if(data< loaded_aliases[alias])
						loaded_aliases[alias]--;
			}
		}
	};
})().init();
